package com.anyou.mall.partner.controller;

import com.anyou.common.annotation.Log;
import com.anyou.common.core.controller.BaseController;
import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.core.page.PageDomain;
import com.anyou.common.core.page.TableDataInfo;
import com.anyou.common.core.page.TableSupport;
import com.anyou.common.enums.BusinessType;
import com.anyou.common.utils.DateUtils;
import com.anyou.common.utils.poi.ExcelUtil;
import com.anyou.common.vo.StartEnd;
import com.anyou.mall.partner.domain.PartnerLavel;
import com.anyou.mall.partner.dto.MyRankDto;
import com.anyou.mall.partner.dto.PartnerRankingDto;
import com.anyou.mall.partner.service.IPartnerLavelService;
import com.anyou.mall.partner.vo.PartnerRankingVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;


/**
 * 合伙人等级Controller
 *
 * @author ruoyi
 * @date 2023-10-10
 */
@Api(tags = "合伙人排名")
@RestController
@RequestMapping("/partner/ranking")
public class PartnerRankingController extends BaseController
{
    @Autowired
    private IPartnerLavelService partnerLavelService;

    /**
     * 合伙人排名列表
     */
    @ApiOperation("查询合伙人排名列表")
    @PreAuthorize("@ss.hasPermi('partner:ranking:list')")
    @GetMapping("/list")
    public TableDataInfo<List<PartnerRankingVo>> list(PartnerRankingDto param)
    {
        //计算类型的日期区间
        jsDate(param);
        startPage();
        List<PartnerRankingVo> list = partnerLavelService.selectPartnerRankingList(param);
        //计算排序编号
        jsNumber(list);
        return getDataTable(list);
    }

    /**
     * 合伙人排名列表
     */
    @ApiOperation("我的排名")
    @PreAuthorize("@ss.hasPermi('partner:ranking:myRank')")
    @GetMapping("/myRank")
    public ApiResult myRank(MyRankDto param)
    {
        Integer rank=partnerLavelService.getMyRank(param.getCarPartShopId());


        return ApiResult.success(0);
    }


    /**
     * 计算类型的日期区间
     * 类型:1-今日，2-昨日，3-本周，4-本月
     * @param param
     */
    private void jsDate(PartnerRankingDto param) {
        if(param.getType()!=null){
            StartEnd se = DateUtils.getStartEndByType(param.getType());
            param.setBeginCreateTime(se.getStartTime());
            param.setEndCreateTime(se.getEndTime());
        }

    }

    /**
     * 计算排序编号
     * @param list
     */
    private void jsNumber(List<PartnerRankingVo> list) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        Integer startRow = (pageNum - 1) * pageSize+1;
        Integer endRow = startRow * pageSize;
        for(PartnerRankingVo prv:list){
            prv.setRankingNo(startRow+"");
            startRow=startRow+1;
        }

    }


}
